package uestc.lj.gulishop.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.Query;
import uestc.lj.gulishop.coupon.dao.SeckillSessionDao;
import uestc.lj.gulishop.coupon.entity.SeckillSessionEntity;
import uestc.lj.gulishop.coupon.entity.SeckillSkuRelationEntity;
import uestc.lj.gulishop.coupon.service.SeckillSessionService;
import uestc.lj.gulishop.coupon.service.SeckillSkuRelationService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询最近三天的秒杀活动
     *
     * @return
     */
    @Override
    public List<SeckillSessionEntity> getLatestThreeDaysSession() {
        // 1.计算活动时间
        String startTime = startTime();
        String endTime = endTime();

        List<SeckillSessionEntity> seckillSessionEntities = this.list(new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime, endTime));

        if (seckillSessionEntities != null && seckillSessionEntities.size() > 0) {
            // 2.查询秒杀项
            List<SeckillSessionEntity> sessionEntities = seckillSessionEntities.stream().map(session -> {
                // 获取当前活动的id
                Long sessionId = session.getId();
                // 根据活动的id查询活动所涉及的商品
                List<SeckillSkuRelationEntity> seckillSkuRelationEntityList = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id"
                        , sessionId));
                session.setSeckillSkuRelationEntities(seckillSkuRelationEntityList);
                return session;
            }).collect(Collectors.toList());
            return sessionEntities;
        }
        return null;
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        return LocalDateTime.of(now, min).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate after = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        return LocalDateTime.of(after, max).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}